//  File Upload Meter - DEMO and API
//  http://abhinavsingh.com/webdemos/fileuploadmeter/fileuploadmeter.rar
//  http://abhinavsingh.com/blog/2008/08/how-to-create-an-upload-meter-in-php-explained/
//  Developer : Abhinav Singh
//  Contact : admin@abhinavsingh.com
//  Date : 29th August, 2008

//  NOTE: Kindly keep this part of the code untouched if you are using or copying the code from here. 
//  Thanks in advance,
//  Abhinav
function getProgress() {
  var xmlhttp = false;
  var id = document.getElementById('fileKey').value; 
  var url = "http://localhost/FileUploadMeter/progress.php?id=" + id + "&nc=" + (Math.random()*100000);
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(oc) {
      xmlhttp = null;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
    xmlhttp = new XMLHttpRequest();
  }
  if(!xmlhttp) {
    document.getElementById('xmlhttp-note').style.display = 'inline';
    return false;
  }
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState!=4) {
    }
    if ((xmlhttp.readyState==4)&&(xmlhttp.status == 200)) {
      var response = xmlhttp.responseText;
      if(response != "") {
        if(response != "Success") {
          var d = eval("(" + response + ")");
          // Update Progress Bar
          if(d.total != 0 && d.current != 0) {
            document.getElementById('showProgress').style.display = 'block';
            var percentDone = (d.current/d.total)*100;
            // Calculate the length of the uploaded file
            var percentLength = parseInt(document.getElementById('showProgress').style.width)*percentDone/100;
            // Update the length of uploaded file
            document.getElementById('currentStatus').style.width = Math.round(percentLength) + 'px';
            if(d.current < d.total) {
              setTimeout("getProgress();",1000);
            }
            else {
              document.getElementById('showProgress').style.display = 'none';
            }
          }
        }
        else {
          document.getElementById('showProgress').style.display = 'none';
        }
      }
      else {
        if(document.getElementById('upload-note').innerHTML == '') {
          setTimeout("getProgress();",1000);
        }
      }
    }
  }
  xmlhttp.open('GET',url,true);
  xmlhttp.send(null);
}
